package com.itheima.exerciseTest.test30_39;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;

/*
    分析以下需求，并用代码实现：
(1)统计每个单词出现的次数
(2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(3)打印格式：
	to=3
		think=1
		you=2
		//........
 */
public class Test34 {
    public static void main(String[] args) {
        HashMap<String,Integer> hm = new HashMap<>();
        TreeMap<String,Integer> tm = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                int result = s1.compareTo(s2);
                return result;
            }
        });
        String str = "If you want to change your fate I think you must come to the dark horse to learn java";
        String[] arr = str.split(" ");
        System.out.println(Arrays.toString(arr));
        for (int i = 0; i < arr.length; i++) {
            String tamp = arr[i];
            int count = 0;
            for (int j = 0; j < arr.length; j++) {
                if (tamp.equals(arr[j]))
                    count++;
            }
            hm.put(tamp,count);
            tm.put(tamp,count);
        }
        System.out.println(hm);
        System.out.println(tm);
    }
}
